//
//  SearchShopsTableViewController.m
//  Sydeny
//
//  Created by Rainbow on 8/14/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SearchShopsTableViewController.h"
#import "CommonShopsTableViewController.h"


@implementation SearchShopsTableViewController
@synthesize searchType;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [searchType release];
    searchType = nil;
    [searchBar release];
    searchBar = nil;
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.title = @"搜索";
    
    loadFinish = YES;
    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    searchBar.delegate = self;
    UIView *searchView = [searchBar.subviews objectAtIndex:0];
	UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"search_bg.png"]];
	imageView.frame = CGRectMake(0, 0, 320, 44);
	[searchView addSubview:imageView];
	
    [self.view addSubview:searchBar];
    
    self.tableView.frame = CGRectMake(10, 44, 300, 392);
    
    UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 196)] autorelease];
    recommandBgView.frame = CGRectMake(0, 10, 300, 176);
    [headerView addSubview:recommandBgView];
    self.tableView.tableHeaderView = headerView;
    
    recommendTableView.delegate = self;
    recommendTableView.dataSource = self;
    
    UITapGestureRecognizer *maskTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
    maskTapGestureRecognizer.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:maskTapGestureRecognizer];
    [maskTapGestureRecognizer release];

    UIButton *backBtn = [[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 30)] autorelease];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"back_btn.png"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(tapBackBtn) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setTitle:@"返回" forState:UIControlStateNormal];
    [backBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
    backBtn.titleLabel.font = [UIFont boldSystemFontOfSize:12];
    UIBarButtonItem *backItem = [[[UIBarButtonItem alloc] initWithCustomView:backBtn] autorelease];
    self.navigationItem.leftBarButtonItem = backItem;
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (NSInteger)tableView:(UITableView *)_tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    if (_tableView.tag == 100) {
        return 4;
    }
    else
        return [super tableView:_tableView numberOfRowsInSection:section];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_tableView.tag == 100) {
        
        UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
        
        NSString *mostStr = @"";
        switch (indexPath.row) {
            case 0:
                mostStr = @"City";
                break;
            case 1:
                mostStr = @"北区";
                break;
            case 2:
                mostStr = @"南区";
                break;
            case 3:
                mostStr = @"西区";
                break;
            default:
                break;
        }
        
        UILabel *nameLabel = [[[UILabel alloc] initWithFrame:CGRectMake(30, 12, 100, 20)] autorelease];
        nameLabel.backgroundColor = [UIColor clearColor];
        nameLabel.text = mostStr;
        [cell.contentView addSubview:nameLabel];
        
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }
    else
    {
        return [super tableView:_tableView cellForRowAtIndexPath:indexPath];
    }
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)_tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_tableView.tag == 100) {
        NSMutableDictionary *params = [[[NSMutableDictionary alloc] initWithCapacity:0] autorelease];
        [params setObject:@"1" forKey:@"page"];
        [params setObject:[NSString stringWithFormat:@"%d",PER_PAGE_NUMBER] forKey:@"limit"];
        [params setObject:searchType forKey:@"type"];
        [params setObject:[NSString stringWithFormat:@"%d", indexPath.row+1] forKey:@"area"];
        NSString *title = @"";
        switch (indexPath.row) {
            case 0:
                title = @"City";
                break;
                
            case 1:
                title = @"北区";
                break;
                
            case 2:
                title = @"南区";
                break;
                
            case 3:
                title = @"西区";
                break;
                
            default:
                break;
        }
        
        CommonShopsTableViewController *commonShopsTableViewController = [[CommonShopsTableViewController alloc] initWithRequsetParams:params withTitle:title RequestType:SydenyRequestGetShops];
        [self.navigationController pushViewController:commonShopsTableViewController animated:YES];
        [commonShopsTableViewController release];
        
        [_tableView deselectRowAtIndexPath:indexPath animated:YES];
        
    }
    else
        return [super tableView:_tableView didSelectRowAtIndexPath:indexPath];
}


- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if ([@"" isEqualToString:searchText]) {
        UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 196)] autorelease];
        recommandBgView.frame = CGRectMake(0, 10, 300, 176);
        [headerView addSubview:recommandBgView];
        self.tableView.tableHeaderView = headerView;
        return;
    }
    else
    {
        self.loadFinish = NO;
        [self.shops removeAllObjects];
        [self.tableView reloadData];
        self.tableView.tableHeaderView = nil;
    }
    [requestParams setObject:@"1" forKey:@"page"];
    [requestParams setObject:[NSString stringWithFormat:@"%d",PER_PAGE_NUMBER] forKey:@"limit"];
    if (searchType != nil) {
        [requestParams setObject:searchType forKey:@"type"];
    }
    [requestParams setObject:searchText forKey:@"keyword"];
    [[ServiceHelper sharedService] requestServerForMethod:SydenyRequestGetShops WithParameters:requestParams AndCallBackScope:self AndSuccessSel:@"getShopsSuccess:" AndFailedSel:@"getShopsFailed:"];
    
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)_searchBar
{
    [_searchBar resignFirstResponder];
}

- (CGFloat)tableView:(UITableView *)_tableView heightForHeaderInSection:(NSInteger)section
{
    if (_tableView.tag == 100) {
        return 0;
    }
    else
        return [super tableView:_tableView heightForHeaderInSection:section];
}

- (CGFloat)tableView:(UITableView *)_tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_tableView.tag == 100) {
        return 44;
    }
    else
        return [super tableView:_tableView heightForRowAtIndexPath:indexPath];
    
}

- (void)handleTapGesture:(UITapGestureRecognizer *)recognizer{
    [searchBar resignFirstResponder];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isKindOfClass:[UISearchBar class]]) {
        return NO; 
    }
    return YES; 
}

- (void)scrollViewDidScroll:(UIScrollView *)_scrollView
{    
    [searchBar resignFirstResponder];
}

-(void)tapBackBtn
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
